var block_numb;		//扫雷块_游戏层 高/宽
var bomb_numb;		//炸弹数
var BLOCK_SUM;		//扫雷块_游戏层 总砖块数
var oper_arr;		//砖块周围数组

var cache_arr;		//砖块值暂存数组
var sec =0;			//秒钟
var isEnd =false;	//游戏结束标志位
var hasFound =0;	//旗帜标中雷数
var timeFlag;		//时间函数返回值

$("#ctrl ,#pad").css("display" ,"none");
$("#set_but").one("click" ,_create);
$(document).on("click" ,"#pad div" ,doSweep).on("contextmenu" ,"#pad div" ,doFlag);
$(document).contextmenu(function(e){e.preventDefault();});
$(document).on("click" ,"#back" ,_back);
$(document).on("click" ,"#prompt_level div button" ,_prompt);

function _create(){	//砖块构造函数
	//从 难度设置层 读取用户设定的 扫雷块_游戏层 高/宽
	bomb_numb =block_numb =parseInt($("input[name=\"block_numb\"]:checked").val());
	BLOCK_SUM =block_numb  *block_numb;
	oper_arr =[-block_numb ,block_numb ,-1 ,1 ,-block_numb +1 ,-block_numb -1 ,block_numb -1 ,block_numb +1];
	cache_arr =new Array(BLOCK_SUM +1);
	//隐去 难度设置层
	$("#ini_frame").css("display" ,"none");
	$("#ctrl ,#pad").css("display" ,"block");
	//一个一个的构造 砖块标签
	for(var y =0 ;y <block_numb ;y ++){
		for(var x =0 ;x <block_numb ;x ++){
			$("#pad").append("<div class=\"block_unvis\" data-isFlag=\"false\" data-isVis=\"false\" data-no=\""+(y *block_numb +x +1)+"\"><p></p></div>");
			$("#pad div:last-child p").css("display" ,"none");		//隐藏p标签
			if(x ==0) $("#pad div:last-child").css("clear" ,"both");
		}
	}
	//炸弹随机放入砖块
	var ran;
	while(bomb_numb !=0){
		ran =parseInt(Math.random() *BLOCK_SUM +1);
		if($("#pad div:nth-child("+ran+")").children().text() =="") $("#pad div:nth-child("+ran+")").children().text("💣"); else continue;
		bomb_numb --;
	}
	//炸弹砖块 遍历周围数组 得到炸弹周围的提示数
	for(var i =1 ;i <=BLOCK_SUM ;i ++){
		if($("#pad div:nth-child("+i+")").children().text() !="💣") continue;
		var tmp_oper;
		for(var oper =0 ;oper <oper_arr.length ;oper ++){
			tmp_oper =parseInt(oper_arr[oper] ,10) +i;
			
			if($("#pad div:nth-child("+tmp_oper+")").children().text() =="💣") continue;
			if(tmp_oper <1 ||tmp_oper >BLOCK_SUM) continue;
			
			if(oper ==2 ||oper ==5 ||oper ==6) {if((i -1) %block_numb ==0) continue;}
			else if(oper ==3 ||oper ==4 ||oper ==7) {if(i %block_numb ==0) continue;}

			if($("#pad div:nth-child("+tmp_oper+")").children().text() =="") $("#pad div:nth-child("+tmp_oper+")").children().text("1"); 
			else  $("#pad div:nth-child("+tmp_oper+")").children().text(parseInt($("#pad div:nth-child("+tmp_oper+")").children().text()) +1);
		}
	}
	//启动 计时函数
	clearInterval(timeFlag);
	timeFlag =setInterval(time ,1000);
}

function doSweep(){	//扫雷函数
	
	if($(this).attr("data-isFlag") =="true") $(this).trigger("contextmenu");
	//该砖块已被翻开
	if($(this).attr("data-isVis") =="true") return ; else $(this).attr("data-isVis" ,"true").addClass("block_vis").children().css("display" ,"block");
	//判定翻开砖块的值
	var no =parseInt($(this).attr("data-no"));
	if($(this).children().text() !=""){
		if($(this).children().text() =="💣") _fail(no);	else $(this).children().addClass("block_vis_"+$(this).children().text());
		return;
	}
	//该砖块为空,向周围扩散扫描
	var tmp_oper;
	for(var oper =0 ;oper <oper_arr.length ;oper ++){
		tmp_oper =parseInt(oper_arr[oper] ,10) +no;
		//扩散到炸弹,停下来
		if($("#pad div:nth-child("+tmp_oper+")").children().text() =="💣") continue;
		//扩散越界
		if(tmp_oper <1 ||tmp_oper >BLOCK_SUM) continue;
		if(oper ==2 ||oper ==5 ||oper ==6) {if((no -1) %block_numb ==0) continue;}	else if(oper ==3 ||oper ==4 ||oper ==7) {if(no %block_numb ==0) continue;}
		//合法扩散
		$("#pad div:nth-child("+tmp_oper+")").trigger("click");
	}
}

function _fail(no_bomb){	//游戏结束函数
	isEnd =true;
	for(var i =1 ;i <=BLOCK_SUM ;i ++) {
		//核算标中雷数
		if(! ($("#pad div:nth-child("+i+")").children().text() =="💣" ||cache_arr[i] =="💣")) continue;
		$("#pad div:nth-child("+i+")").addClass("block_vis").children().css("display" ,"block");
		if($("#pad div:nth-child("+i+")").attr("data-isFlag") =="true") 	$("#pad div:nth-child("+i+")").css("background-color" ,"green").children().text(cache_arr[i]);
	}
	$("#pad div:nth-child("+parseInt(no_bomb)+")").css("background-color" ,"red");
	//解绑左右键单击事件
	$(document).off("click" ,"#pad div").off("contextmenu" ,"#pad div");
	//切换bgm
	$("#voi").attr("src" ,"voi/failed.mp3");
}

function doFlag(){	//旗帜标志函数
	var no_block =$(this).attr("data-no");
	//标中则值入暂存数组
	if($(this).attr("data-isFlag") =="false") {
		cache_arr[parseInt(no_block)] =$(this).children().text();
		if(cache_arr[parseInt(no_block)] =="💣") hasFound ++;
		$(this).attr("data-isFlag" ,"true").children().text("🚩").css("display" ,"block"); 
	}else {
		if(cache_arr[parseInt(no_block)] =="💣") hasFound --;
		$(this).attr("data-isFlag" ,"false").children().text(cache_arr[no_block]).css("display" ,"none");
		cache_arr[no_block] ="";
	}
}

function time(){	//计时函数
	if(isEnd ||hasFound ==block_numb) {
		clearInterval(timeFlag);
		$(document).off("click" ,"#back");
		//显示 提示层
		$($("#pad ,#ctrl")).css("opacity" ,"0.3");	$("#prompt_level").css("display" ,"block");
		if(isEnd) {$("#prompt_level div p").text("踩到雷咯 ,这波是再接再励...");} else {$("#prompt_level div p").text("成功通关 ,用时 : "+sec+" s"); $(document).off("click" ,"#pad div").off("contextmenu" ,"#pad div"); $("#voi").attr("src" ,"voi/vectory.mp3");}
		$("#voi").removeAttr("loop");
	}
	$("#clock").text(sec);
	sec ++;
}
//确认块_提示层 绑定 提示层 隐去事件
function _prompt(){$(document).on("click" ,"#back" ,_back); $("#pad ,#ctrl").css("opacity" ,"1");	$("#prompt_level").css("display" ,"none");}
//退出
function _back(){location.reload(true);}
